
capture program drop topreform
program define topreform
args income tax inc poor rich

/* This program simulates a reform which increases the relative participation in tax liabilities of the richest, defined in the dataset as nivel=1 (nivel=0 are the "poor")
income = variable with taxable income
cuota = variable with tax liability
inc = increase in pp of the participation of the rich
poor = way to allocate the tax reduction to the poor:
	0 = no modification of the liabilities
	1 = proporational to individual tax liabilities
	2 = proportional to individual after-tax income
	3 = proportional to individul taxable income
rich = way to allocate the tax reduction to the rich
	[values as for the poor, except 0]
If poor==0 | rich==0 => reform is not revenue-neutral
If poor!=0 & rich!=0 => reform is revenue-neutral
*/

quiet{

*-------------------------- DELETE AND GENERATE VARIABLES

capture drop gross_income
capture drop tax_old 
capture drop tax_new 
capture drop net_income_old
capture drop net_income_new
capture drop tme_old 
capture drop tme_new

gen gross_income=`income'
gen tax_old=`tax'
gen net_income_old=gross_income-tax_old


*-------------------------- CALCULATION OF THE TARGET REVENUE FOR EACH GROUP (it only depends on poor==0 or poor!=0)

sum tax_old if nivel==0 [iw=factor]
local revenue_poor=r(sum)
nois disp "Revenue poor: " %15.0f `revenue_poor'

sum tax_old if nivel==1 [iw=factor]
local revenue_rich=r(sum)
nois disp "Revenue rich: " %15.0f `revenue_rich'

sum tax_old [iw=factor]
local revenue_total=r(sum)
nois disp "Revenue total: " %15.0f `revenue_total'

local prop_poor=`revenue_poor'/`revenue_total'
nois disp "% revenue poor: " `prop_poor'*100

local prop_rich=`revenue_rich'/`revenue_total'
nois disp "% revenue rich: " `prop_rich'*100

nois disp ""

if `poor'==0{
local new_revenue_poor =`revenue_poor'
nois disp "New revenue poor: "  %15.0f `new_revenue_poor'
}

if `poor'!=0{
local new_revenue_poor=`revenue_total'*(`prop_poor'-`inc'/100)
nois disp "New revenue poor: "  %15.0f `new_revenue_poor'
}

local new_revenue_rich=((`prop_rich'+`inc'/100)/(1-(`prop_rich'+`inc'/100)))*`new_revenue_poor'
nois disp "New revenue rich: "  %15.0f `new_revenue_rich'

local new_revenue_total=`new_revenue_poor'+`new_revenue_rich'
nois disp "New revenue total: " %15.0f `new_revenue_total'

local new_prop_poor=`new_revenue_poor'/`new_revenue_total'
nois disp "New % revenue poor: " `new_prop_poor'*100
local new_prop_rich=`new_revenue_rich'/`new_revenue_total'
nois disp "New % revenue rich: " `new_prop_rich'*100




*-------------------------- ASSIGN THE LIABILITIES DEPENDING ON THE PARAMETERS

gen double tax_new=.




*************** POOR

if `poor'==0{
replace tax_new=tax_old if nivel==0
}

sum gross_income if nivel==0 [iw=factor]	/* It's necessary later on */
local income_poor=r(sum)

if `poor'==1{
*replace tax_new=tax_old+(`new_revenue_poor'-`revenue_poor')*(tax_old/`revenue_poor') if nivel==0
replace tax_new=tax_old*(1+(`new_revenue_poor'-`revenue_poor')/`revenue_poor') if nivel==0
}

if `poor'==2{
replace tax_new=tax_old+(`new_revenue_poor'-`revenue_poor')*((gross_income-tax_old)/(`income_poor'-`revenue_poor')) if nivel==0
}

if `poor'==3{
replace tax_new=tax_old+(`new_revenue_poor'-`revenue_poor')*(gross_income/`income_poor') if nivel==0
}





*************** RICH

sum gross_income if nivel==1 [iw=factor]	/* It's necessary later on */
local income_rich=r(sum)

if `rich'==1{
*replace tax_new=tax_old+(`new_revenue_rich'-`revenue_rich')*(tax_old/`revenue_rich') if nivel==1
replace tax_new=tax_old*(1+(`new_revenue_rich'-`revenue_rich')/`revenue_rich') if nivel==1
}

if `rich'==2{
replace tax_new=tax_old+(`new_revenue_rich'-`revenue_rich')*((gross_income-tax_old)/(`income_rich'-`revenue_rich')) if nivel==1
}

if `rich'==3{
replace tax_new=tax_old+(`new_revenue_rich'-`revenue_rich')*(gross_income/`income_rich') if nivel==1
}






*-------------------------- CALCULATE THE AVERAGE EFFECTIVE TAX RATES BEFORE AND AFTER THE REFORM

gen net_income_new=gross_income-tax_new

gen tme_old=tax_old/gross_income
	replace tme_old=0 if tme_old==.
gen tme_new=tax_new/gross_income
	replace tme_new=0 if tme_new==.

nois disp ""

sum tax_old [iw=factor] if nivel==0
nois disp "Average tax new poor: " r(mean)
sum tax_new [iw=factor] if nivel==0
nois disp "Average tax new poor: " r(mean)

sum tax_old [iw=factor] if nivel==1
nois disp "Average tax new rich: " r(mean)
sum tax_new [iw=factor] if nivel==1
nois disp "Average tax new rich: " r(mean)

sum tax_old [iw=factor]
nois disp "Average tax old all: " r(mean)
sum tax_new [iw=factor]
nois disp "Average tax new all: " r(mean)


nois disp ""

sum net_income_old [iw=factor] if nivel==0
nois disp "Average net_income new poor: " r(mean)
sum net_income_new [iw=factor] if nivel==0
nois disp "Average net_income new poor: " r(mean)

sum net_income_old [iw=factor] if nivel==1
nois disp "Average net_income new rich: " r(mean)
sum net_income_new [iw=factor] if nivel==1
nois disp "Average net_income new rich: " r(mean)

sum net_income_old [iw=factor]
nois disp "Average net_income old all: " r(mean)
sum net_income_new [iw=factor]
nois disp "Average net_income new all: " r(mean)

nois disp ""

sum tme_old [iw=factor] if nivel==0
nois disp "Average tme new poor: " r(mean)
sum tme_new [iw=factor] if nivel==0
nois disp "Average tme new poor: " r(mean)

sum tme_old [iw=factor] if nivel==1
nois disp "Average tme new rich: " r(mean)
sum tme_new [iw=factor] if nivel==1
nois disp "Average tme new rich: " r(mean)

sum tme_old [iw=factor]
nois disp "Average tme old all: " r(mean)
sum tme_new [iw=factor]
nois disp "Average tme new all: " r(mean)

nois disp ""

nois disp "POOR"

ginis net_income_old [aw=factor] if nivel==0
nois disp "Gini renta neta old: " $ginis

ginis net_income_new [aw=factor] if nivel==0
nois disp "Gini renta neta new: " $ginis

ginis tax_old [aw=factor] if nivel==0
nois disp "Gini tax old: " $ginis

ginis tax_new [aw=factor] if nivel==0
nois disp "Gini tax new: " $ginis

nois disp "RICH"

ginis net_income_old [aw=factor] if nivel==1
nois disp "Gini renta neta old: " $ginis

ginis net_income_new [aw=factor] if nivel==1
nois disp "Gini renta neta new: " $ginis

ginis tax_old [aw=factor] if nivel==1
nois disp "Gini tax old: " $ginis

ginis tax_new [aw=factor] if nivel==1
nois disp "Gini tax new: " $ginis

nois disp "ALL"

ginis gross_income [aw=factor] 
	local ginigross=$ginis
	nois disp "Gini renta: " `ginigross'

ginis net_income_old [aw=factor] 
	local gininetold=$ginis
	nois disp "Gini renta neta old: " `gininetold'

ginis net_income_new [aw=factor] 
	local gininetnew=$ginis
	nois disp "Gini renta neta new: " `gininetnew'

ginis tax_old [aw=factor] 
	local ginitaxold=$ginis
	nois disp "Gini tax old: " `ginitaxold'

ginis tax_new [aw=factor] 
	local ginitaxnew=$ginis
	nois disp "Gini tax new: " `ginitaxnew'
	
local kakwaniold=`ginitaxold'-`ginigross'
	nois disp "Kakwani old: " `kakwaniold'

local kakwaninew=`ginitaxnew'-`ginigross'
	nois disp "Kakwani new: " `kakwaninew'

local rsold=`ginigross'-`gininetold'
	nois disp "RS old: " `rsold'

local rsnew=`ginigross'-`gininetnew'
	nois disp "RS new: " `rsnew'	

}

end

*topreform renta cral_sty 9 1 1	// test
